﻿#region LicenseHeader

// Copyright 2012 The Trustees of Indiana University.  All rights reserved.
// 
// The Indiana University licenses this file to you under the Apache License, 
// Version 2.0 (the "License"); you may not use this file except in compliance 
// with the License.  You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
// See the License for the specific language governing permissions and 
// limitations under the License.
// 
// @author Thilina Gunarathne (tgunarat@indiana.edu)

#endregion

using System;
using System.Diagnostics;
using System.ServiceModel;
using System.Threading.Tasks;
using AzureMRCore.Primitives;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace AzureMRCore.DataCommunication
{
    /// <summary>
    ///   Implementation of the WCF chat service.
    /// </summary>
    [ServiceBehavior(
        InstanceContextMode = InstanceContextMode.Single,
        ConcurrencyMode = ConcurrencyMode.Multiple,
#if DEBUG
        IncludeExceptionDetailInFaults = true,
#else
        IncludeExceptionDetailInFaults = false,
#endif
        AddressFilterMode = AddressFilterMode.Any)]
    public class DataXferService : IDataXferService
    {
        /// <summary>
        ///   Called by clients to announce they are connected at this chat endpoint.
        /// </summary>
        /// <param name="item"> </param>
        /// <returns> The ClientInformation object for the new session. </returns>
        public void Broadcast(DataItem item)
        {
            BCastServiceReceiver bCastServiceReceiver = BCastServiceReceiver.GetInstance();
            switch (item.TransferType)
            {
                case Types.AllGather:
                    bCastServiceReceiver.AddAllGatherData(item);
                    break;
                case Types.SimpleBCast:
                    bCastServiceReceiver.AddSimpleBCastData(item);
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

//            Trace.WriteLine("got data product  " + item.WorkerID + " " + item.MapIndex + " " + item.JobID + " " +
//                            item.Iteration);
        }

        public void TreeBroadcast(TreeBCastItem item, int min, int max, int maxBroadcasters)
        {
            Trace.WriteLine("Got TreeBCast data." + RoleEnvironment.CurrentRoleInstance.Id + "  min" + min + "  max" +
                            max);
            if (item.Recurse)
            {
                MRConf mrConf = MRConf.GetInstance();
                var task =
                    new Task(
                        () => DataTransferClient.TreeBroadcast(item, min, max, mrConf.Factory, maxBroadcasters));
                task.Start();
            }

            BCastServiceReceiver bCastServiceReceiver = BCastServiceReceiver.GetInstance();
            bCastServiceReceiver.AddSimpleBCastData(item);

//            Trace.WriteLine("got data product  " + item.WorkerID + " " + item.MapIndex + " " + item.JobID + " " +
//                            item.Iteration);
        }

        public void PutReduceData(DataItem item)
        {
            var reduceReceiver = ReduceDataReceiver.GetInstance();
            reduceReceiver.AddData(item);
        }
    }
}